package com.willpower.eureka.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.willpower.eureka.auth.response.CustomizeResultCode;
import com.willpower.eureka.auth.response.Result;
import com.willpower.eureka.auth.response.ResultCode;
import com.willpower.eureka.entity.SysMenu;
import com.willpower.eureka.entity.SysUser;
import com.willpower.eureka.service.ISysMenuService;
import com.willpower.eureka.service.ISysUserService;
import com.willpower.eureka.utils.RSAEncrypt;
import com.willpower.eureka.utils.RedisUtil;
import com.willpower.eureka.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.pqc.jcajce.provider.Rainbow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author: wen-yi;
 * @date: 2021/11/2 0:10;
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("api")
public class ApiController {
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ISysMenuService sysMenuService;

    @Autowired
    private ISysUserService sysUserService;

    @RequestMapping("test1")
    public Object test1(@RequestBody JSONObject json){
        log.info("测试跨域问题");
        return json;
    }
    @RequestMapping("test2")
    public Object test2(@RequestParam String username, String password){
        JSONObject json = new JSONObject();
        json.put("username",username);
        json.put("password",password);
        return json;
    }
    @PostMapping("login")
    public Object login(String username, String password){
        log.info("params: {},{}",username,password);
        JSONObject json = result(true, 200, "登录成功", null);
        json.put("token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsaWtlIjoiYWoiLCJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbImFsbCJdLCJhdGkiOiI5MjIxNmE4ZC1hN2YzLTRlNDUtYjI3Mi1mNWQzNGQ1OTEzOTQiLCJleHAiOjE2MzU5NTg5NzUsImF1dGhvcml0aWVzIjpbImFkbWluIiwiUk9MRV9yb290Il0sImp0aSI6Ijc4MTQ2Zjk0LTBiOGMtNGQ2My1iNGRjLTFhZTk5MjU4MjdjOCIsImNsaWVudF9pZCI6ImFkbWluIn0.H8J6kAnEMrRdcafOYS9jJk89CFDFemDXr0KZg7UFX3E");
        return json;
    }

    @GetMapping("menus")
    public Object menus(){
        List<SysMenu> menus = sysMenuService.getAllMenu();
        return result(true, 200, "成功", menus);
    }

    @GetMapping("users")
    public JSONObject users(@RequestParam(defaultValue = "") String query,@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "2") Integer pageSize){
        log.info("query: {}, pageNum: {}, pageSize: {}", query,pageNum,pageSize);
        return sysUserService.selectPage(query,pageNum,pageSize);
    }

    @GetMapping("users/{id}/enabled/{enabled}")
    public JSONObject usersEnabled(@PathVariable("id") Integer id,@PathVariable("enabled") Boolean enabled){
        log.info("id: {}, enabled: {}", id,enabled);
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getEnabled,enabled);
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
//        sysUserService.update(sysUser,wrapper);
        return null;
    }

    public JSONObject result(Boolean status,Integer code,String msg,Object data){
        JSONObject json = new JSONObject();
        json.put("status", status);
        json.put("code", code);
        json.put("msg", msg);
        json.put("data", data);
        return json;
    }

    @PostMapping("getPublicKey")
    public Result getPublicKey() {
        try {
            RSAEncrypt.initKey();
            String publicKey = RSAEncrypt.getPublicKey(RSAEncrypt.PUBLIC_KEY);//获得公钥
            // System.out.println(publicKey);
            String privateKey = RSAEncrypt.getPrivateKey(RSAEncrypt.PRIVATE_KEY);//获得私钥
            redisUtil.setEx(RSAEncrypt.PRIVATE_KEY, privateKey, 10, TimeUnit.MINUTES);
            return Result.ok(publicKey);
        } catch (Exception e) {
            log.error("getPublicKey error", e);
            return Result.fail(ResultCode.ENC_KEY_FAIL);
        }
    }

    @PostMapping("getEncKey")
    public Result getEncKey() {
        try {
            String encKey =  UUID.randomUUID().toString().replaceAll("-", "");
            redisUtil.setEx("encKey", encKey, 30, TimeUnit.SECONDS);
            return Result.ok(encKey);
        } catch (Exception e) {
            log.error("getPublicKey error", e);
            return Result.fail(ResultCode.ENC_KEY_FAIL);
        }
    }
}
